package com.xixi.controller;

import com.xixi.model.domain.User;
import com.xixi.model.request.UserLoginRequest;
import com.xixi.model.request.UserRegisterRequest;
import com.xixi.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author zxh
 * @date 2024/9/11 9:46
 * @description 用户接口
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	@PostMapping("register")
	public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
		if (userRegisterRequest == null) {
			return null;
		}
		String userAccount = userRegisterRequest.getUserAccount();
		String userPassword = userRegisterRequest.getUserPassword();
		String checkPassword = userRegisterRequest.getCheckPassword();
		if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
			return null;
		}
		return userService.userRegister(userAccount, userPassword, checkPassword);
	}

	@PostMapping("login")
	public User userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
		if (userLoginRequest == null) {
			return null;
		}
		String userAccount = userLoginRequest.getUserAccount();
		String userPassword = userLoginRequest.getUserPassword();
		if (StringUtils.isAnyBlank(userAccount, userPassword)) {
			return null;
		}
		return userService.doLogin(userAccount, userPassword, request);
	}

	@GetMapping("/search")
	public List<User> searchUser(String username, HttpServletRequest request) {
		return userService.searchUser(username, request);
	}

	@PostMapping("/delete")
	public boolean deleteUser(@RequestBody Long userId, HttpServletRequest request) {
		if (userId <= 0) {
			return false;
		}
		return userService.deleteUser(userId, request);
	}

}
